<?php


namespace App\Http\Controllers\Api\Repositories;


use App\Dengje\Utils\Settings;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxUserCircle;
use App\Models\WxCircle;
use App\Models\WxUser;
use App\Models\WxNotice;
use App\Models\WxCircle as Model;


class WxCircleRepositores
{


    public static function add($data)
    {
        $model = new Model();
        if ($data['id']) {
            $circle = $model->where('id', $data['id'])->first();
            $circle->circle_name = $data['circle_name'];
            $circle->circle_introduce = $data['circle_introduce'];
            $circle->head_portrait = $data['head_portrait'];
            $circle->background_maps = $data['background_maps'];
            $circle->plate_id = $data['plate_id'];
            // 判断是否需要审核新建的圈子
            if (Settings::get('is_audit_create_cirlce') == 1) {
                $circle->circle_state = 1;
            }
            return $circle->save();
        } else {
            $model->circle_name = $data['circle_name'];
            $model->circle_introduce = $data['circle_introduce'];
            $model->head_portrait = $data['head_portrait'];
            $model->background_maps = $data['background_maps'];
            $model->plate_id = $data['plate_id'];
            $model->user_id = $data['uid'];
            // 判断是否需要审核新建的圈子
            if (Settings::get('is_audit_create_cirlce') == 1) {
                $model->circle_state = 1;
            }
            return $model->save();
        }

    }

    /**
     * -1:最火;0:最新
     * @param $plateId
     * @return mixed
     */
    public static function circleByPlateId($plateId)
    {
        if ($plateId == 0) {
            return (new Model())
                ->where('circle_state', 1)
                ->withCount('wxPosts as posts_count')
                ->withCOunt('wxUserCircle as user_circle_count')
                ->orderBy('created_at', 'DESC')
                ->limit(20)
                ->get();

        } elseif ($plateId == -1) {
            return (new Model())
                ->where('circle_state', 1)
                ->withCount('wxPosts as posts_count')
                ->withCOunt('wxUserCircle as user_circle_count')
                ->orderBy('sort', 'asc')
                ->limit(10)
                ->get();
        }
        return (new Model())
            ->where('plate_id', $plateId)
            ->where('circle_state', 1)
            ->withCount('wxPosts as posts_count')
            ->withCOunt('wxUserCircle as user_circle_count')
            ->orderBy('sort', 'asc')
            ->get();
    }

    /**
     * 搜索
     * @param $keyword
     * @return mixed
     */
    public static function searchCircle($keyword, $uid = 0, $limit = 10)
    {
        $data = (new Model())
            ->where('circle_state', 1)
            ->where(function ($query) use ($keyword) {
                $query->orWhere('circle_name', 'like', '%' . $keyword . '%')
                    ->orWhere('circle_introduce', 'like', '%' . $keyword . '%');
            })
            ->withCount('wxPosts as posts_count')
            ->withCount('wxUserCircle as user_circle_count')
            ->simplePaginate($limit);
        $data->map(function ($item) use ($uid) {
            $item->is_follow = (new WxUserCircle())->isFollowCircle($uid, $item->id);
        });
        return $data;

    }

    /**
     * 推荐
     * @return mixed
     */
    public static function recommendCircle()
    {
        return (new Model())->where('circle_state', 1)->where('is_top_recommend', 1)->withCount('wxPosts as posts_count')->orderBy('sort', 'asc')->limit(4)->get();
    }

    /**
     * 热门
     * @return mixed
     */
    public static function hotCircle()
    {
        return (new Model())->where('circle_state', 1)->where('is_hot', 1)->orderBy('sort', 'asc')->limit(12)->get();
    }

    /**
     * 最新
     * @return mixed
     */
    public static function newCircle()
    {
        return (new Model())->where('circle_state', 1)->where('circle_state', 0)->limit(12)->orderBy('id', 'desc')->get();
    }

    /**
     * 全部
     * @param $page
     * @return mixed
     */
    public static function list($page)
    {
        return (new Model())->where('circle_state', 1)->where('circle_state', 0)->orderBy('id', 'desc')->paginate($page);
    }

    /**
     * 圈子文章组合(有的问题with limit)
     * @param $page
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function circleAndPost($limit = 10)
    {
        $data = (new Model())->where('circle_state', 1)
            ->withCount(['wxPosts as posts_count' => function ($query) {
                $query->where('is_examine', 1);
            }])
            ->withCount('wxUserCircle as user_circle_count')
            ->orderBy('sort', 'asc')
            ->simplePaginate($limit);
        $data->map(function ($v, $k) {
            $data = WxPost::where('is_examine', 1)
                ->where('posts_state', 0)
                ->where('circle_id', $v->id)
                ->orderBy('id', 'desc')
                ->with(["user" => function ($query) {
                    $query->select(['id', 'user_avatar']);
                }])
                ->get(['id', 'posts_content', 'user_id', 'circle_id']);
            $data->map(function ($v) {
                $v->commment_count = WxCommentRepositores::commentCount($v->id);
            });
            $v->wx_posts = $data;
        });
        return $data;

    }

    /**
     * 用户关注圈子
     * @param $user_id
     * @param $circle_id
     * @return bool
     */
    public static function userFollowCircle($user_id, $circle_id)
    {

        $model = new WxUserCircle();
        if ($model->where('user_id', $user_id)->where('circle_id', $circle_id)->exists()) {
            $model->where('user_id', $user_id)->where('circle_id', $circle_id)->delete();
        } else {
            $model->user_id = $user_id;
            $model->circle_id = $circle_id;


            $user = WxUser::where('id', $user_id)->first('user_name');
            $circle = WxCircle::where('id', $circle_id)->first(['user_id', 'circle_name']);

            (new WxNotice())->addNotice($circle['user_id'], '创建的圈子收获粉丝', $user['user_name'] . '关注了你创建的「' . $circle['circle_name'] . '」圈子。', 0);

            return $model->save();
        }

    }

    /**
     * 用户关注的圈子文章组合列表
     * @param int $limit
     * @return \Illuminate\Contracts\Pagination\Paginator
     */
    public static function userFollowCircleList($limit = 10, $uid)
    {
        $userFollowCircle = WxUserCircle::where('user_id', $uid)->get();
        $circle_id_arr = [];
        foreach ($userFollowCircle as $k => $v) {
            $circle_id_arr[] = $v['circle_id'];
        }
        $data = (new Model())->where('circle_state', 1)
            ->whereIn('id', $circle_id_arr)
            ->withCount(['wxPosts as posts_count' => function ($query) {
                $query->where('is_examine', 1);
            }])
            ->withCount('wxUserCircle as user_circle_count')
            ->orderBy('sort', 'asc')
            ->simplePaginate($limit);
        $data->map(function ($v, $k) {
            $data = WxPost::where('is_examine', 1)
                ->where('posts_state', 0)
                ->where('circle_id', $v->id)
                ->orderBy('id', 'desc')
                ->with(["user" => function ($query) {
                    $query->select(['id', 'user_avatar']);
                }])
                ->get(['id', 'posts_content', 'user_id', 'circle_id']);
            $data->map(function ($v) {
                $v->commment_count = WxCommentRepositores::commentCount($v->id);
            });
            $v->wx_posts = $data;
        });
        return $data;
    }

    /**
     * 获取圈子详细信息
     * @param $circle_id
     * @return mixed
     */
    public static function CircleInfo($circle_id)
    {
        $data = WxCircle::where('id', $circle_id)->first(['id', 'circle_introduce', 'plate_id', 'circle_name', 'background_maps', 'head_portrait', 'user_id']);
        $data->append(['circle_user', 'circle_follow_count', 'circle_posts_count']);
        return $data;
    }

    /**
     * 判断是否关注了该圈子
     */
    public static function isFollowCircle($uid, $circle_id = 0)
    {
        return WxUserCircle::where('user_id', $uid)
            ->where('circle_id', $circle_id)->exists();
    }

    /**
     * 关注圈子
     * @param $uid
     * @param $circle_id
     * @return bool
     */
    public static function followCircle($uid, $circle_id)
    {
        $UserCircleModel = (new WxUserCircle());
        $UserCircleModel->user_id = $uid;
        $UserCircleModel->circle_id = $circle_id;
        return $UserCircleModel->save();
    }


}
